from datetime import datetime, timedelta

from flask import Blueprint, request
from common.models.stu import Sub, Stu, db,User
from flask_restful import Api, Resource, marshal, marshal_with, fields, reqparse

from common.utils.jwt_util import generate_jwt, verify_jwt,redis_cli


stubp = Blueprint('stubp', __name__)
api = Api(stubp)

sub_fileds = {
    "id": fields.Integer,
    "name": fields.String
}

class Register(Resource):
    def post(self):
        pars = reqparse.RequestParser()
        pars.add_argument("name")
        pars.add_argument("pwd")
        pars.add_argument("mobile")
        args = pars.parse_args()
        user_name = args["name"]
        password = args["pwd"]
        mobile = args["mobile"]
        users = User.query.filter_by(mobile=mobile).count()
        if users:
            return {"code":400,"msg":"该手机号已被注册"}
        user = User(user_name = user_name,password=password,mobile=mobile)
        db.session.add(user)
        db.session.commit()

        redis_cli.setex(mobile,2000,"123123")

        return {"code":200,"msg":"注册成功"}

class Login(Resource):
    def post(self):
        pars = reqparse.RequestParser()




# class Login(Resource):
#     def post(self):
#         parse = reqparse.RequestParser()
#         parse.add_argument("name")
#         args = parse.parse_args()
#         payload = {"name":args.name}
#         expire = datetime.utcnow() + timedelta(seconds=2000)
#         token = generate_jwt(payload,expire)
#         return ({'msg': 'login successful', 'token': token})

class Check_token(Resource):
    def post(self):
        parse = reqparse.RequestParser()
        parse.add_argument("token")
        args = parse.parse_args()
        payload = verify_jwt(args.token)
        if payload:
            return payload
        else:
            return 'Token not working'


class StuResource(Resource):
    def get(self):
        return '1111111111111'


class GetSub(Resource):
    @marshal_with(sub_fileds)
    def get(self):
        sub = Sub.query.all()
        stu = sub[0].stu
        return stu


class AddSub(Resource):
    def post(self):
        parse = reqparse.RequestParser()
        parse.add_argument("name")
        args = parse.parse_args()
        sub = Sub(name=args["name"])
        db.session.add(sub)
        db.session.commit()
        return marshal(sub, sub_fileds)


class UpdateSub(Resource):
    def post(self):
        parse = reqparse.RequestParser()
        parse.add_argument("name")
        parse.add_argument("id")
        args = parse.parse_args()
        sub = Sub.query.filter(Sub.id == args["id"]).update(args)
        db.session.commit()
        return marshal(sub, sub_fileds)


class DelSub(Resource):
    def delete(self):
        pares = reqparse.RequestParser()
        pares.add_argument("name")
        pares.add_argument("id")
        args = pares.parse_args()
        id = args.get("id")
        Sub.query.filter(Sub.id == id).delete()
        db.session.commit()
        return "ok"


api.add_resource(StuResource, '/stu', endpoint='stu')
api.add_resource(GetSub, '/get_sub')
api.add_resource(AddSub, '/AddSub')
api.add_resource(UpdateSub, '/update_sub')
api.add_resource(DelSub, '/del_sub')
api.add_resource(Login, '/login')
api.add_resource(Check_token, '/check_token')
api.add_resource(Register, '/register')
